#include "dashboard.h"
#include "ui_dashboard.h"
#include <QDebug>
#include <QPixmap>
#include <QTransform>

DashBoard::DashBoard(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::DashBoard)
{
    ui->setupUi(this);
    pixDir = QPixmap(":/main/image/car_dir.png");
//    pixDir.scaled(50,50);
    dirVal = 0;
    scene = new QGraphicsScene();
    item = new QGraphicsPixmapItem();
    item->setPixmap(pixDir);
    item->setTransformationMode(Qt::SmoothTransformation);
//    item->setOffset(0,0);
//    qDebug()<<item->boundingRect();
    scene->addItem(item);

    qDebug()<<"scene"<<scene->sceneRect();

}

DashBoard::~DashBoard()
{
    delete ui;
}

void DashBoard::carInfo(QStringList info)
{
    if(info.isEmpty())
        return;
    if(info.count()!=7)
        return;

    setSpeed(info.at(0));
    setAcce(info.at(1));
    setBrake(info.at(2));
    setDir(info.at(3));
    setDriveMode(info.at(4));
    setParkingBrake(info.at(5));
    setTrafficLightFlag(info.at(6));
}

void DashBoard::setSpeed(QString speed)
{
    ui->speed->display(speed);
}

void DashBoard::setAcce(QString acce)
{
    ui->acce->setValue(acce.toInt());
}

void DashBoard::setBrake(QString brake)
{
    ui->brake->setValue(brake.toInt());
}

void DashBoard::setDir(QString dir)
{
//    scene->setSceneRect(ui->dir->rect());

    int d = dir.toInt();
    QRectF r = item->boundingRect();
//    qDebug()<<"[rotate]"<<dirVal-d;
    item->setTransform(QTransform().translate(r.width() / 2, r.height() /2).rotate(d, Qt::ZAxis).translate(-r.width() / 2, -r.height() / 2));
//    ui->dir->rotate(dirVal-d);
//    ui->dir->setPixmap(pixDir.transformed(matrix, Qt::SmoothTransformation));
    dirVal = d;
}

void DashBoard::setParkingBrake(QString brake)
{
    if(brake == "TRUE")
    {
        ui->parking_brake->show();
    }
    else if(brake == "FALSE")
    {
        ui->parking_brake->hide();
    }
}

void DashBoard::setTrafficLightFlag(QString light)
{
    if(light == "R")
    {
        ui->light->setStyleSheet(QString("background-color: %1;border:1px solid  rgb(114, 159, 207);border-radius:20px;").arg("rgb(239, 41, 41)"));
    }
    else if(light == "G")
    {
        ui->light->setStyleSheet(QString("background-color: %1;border:1px solid  rgb(114, 159, 207);border-radius:20px;").arg("rgb(115, 210, 22)"));
    }
    else if(light == "Y")
    {
        ui->light->setStyleSheet(QString("background-color: %1;border:1px solid  rgb(114, 159, 207);border-radius:20px;").arg("rgb(252, 175, 62)"));
    }
    else if(light == "N")
    {
        ui->light->setStyleSheet(QString("background-color: %1;border:1px solid  rgb(114, 159, 207);border-radius:20px;").arg("rgb(241, 241, 241)"));
    }
}

void DashBoard::setDriveMode(QString driveMode)
{
    if(driveMode == "AUTO")
    {
        ui->drive_mode->setChecked(true);
    }
    else if(driveMode == "MANU")
    {
        ui->drive_mode->setChecked(false);
    }
}

void DashBoard::on_drive_mode_clicked(bool checked)
{
    emit driveMode(checked);
}

void DashBoard::on_drive_mode_toggled(bool checked)
{
    if(checked)
    {
        ui->drive_mode->setText("自动");
    }
    else
    {
        ui->drive_mode->setText("手动");
    }
}

void DashBoard::resizeEvent(QResizeEvent *)
{
    scene->setSceneRect(ui->dir->rect());
    item->setPos(0,0);
    ui->dir->centerOn(ui->dir->rect().center());
    ui->dir->setScene(scene);
}
